//
//  LoginViewController.m
//  Stream
//
//  Created by shaw on 16/7/31.
//  Copyright © 2016年 showshaw. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "NSString+XX.h"
//测试用  记得删除
#import "MainViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation LoginViewController

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES];
    
//    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
//    
//    [self.view addSubview:closeBtn];
//    
//    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view);
//        make.top.equalTo(self.view);
//    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.password.text = @"1234";
    self.phoneNumber.text = @"1234";

    // setup view
    [self setupView];
    
}


-(void)setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.loginButton.layer.cornerRadius = 3;
    self.loginButton.layer.masksToBounds = YES;
}

//click find password
- (IBAction)didClickedFindPasswordButton:(UIButton *)sender {
    RegisterViewController * registerController = [[[NSBundle mainBundle] loadNibNamed:@"RegisterViewController" owner:self options:nil] firstObject];
    registerController.type = RegisterTypeFindPassword;
    [self.navigationController pushViewController:registerController animated:YES];
}

- (IBAction)didClickedRegisterButton:(UIButton *)sender {
    RegisterViewController * registerController = [[[NSBundle mainBundle] loadNibNamed:@"RegisterViewController" owner:self options:nil] firstObject];
    registerController.type = RegisterTypeRegister;
    [self.navigationController pushViewController:registerController animated:YES];
}

- (IBAction)didClickedLoginButton:(UIButton *)sender {
    NSString *phone = [NSString removeAllWhiteSpace:self.phoneNumber.text];
    NSString *password = [NSString removeAllWhiteSpace:self.password.text];
    
    if ([NSString isBlank:phone]) {
        [MBProgressHUD showMessage:@"账号不能为空" hide:YES];
        return;
    }else if([NSString isBlank:password])
    {
        [MBProgressHUD showMessage:@"密码不能为空" hide:YES];
        return;
    }
    
    [MBProgressHUD showMessage:@""];
    
    if ([self.phoneNumber.text isEqualToString:@"1234"] && [self.password.text isEqualToString:@"1234"]) {
        [MBProgressHUD showSuccess:@"登录成功"];
        [self dismissViewControllerAnimated:YES completion:^{
            [MBProgressHUD hideHUD];
        }];
    }
    else
    {
        [MBProgressHUD showSuccess:@"账号/密码错误！"];
    }
}
#pragma mark - UITextFieldDelegate

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
